<?php

include ("includes/header.php");
include ("includes/connection.php");
include ("includes/contentHeader.php");
?>

<!--contingut 1-->
<section class="container">
	<!--fons gris-->
	<div class="hero-unit">
		<form id="form1" name="form1" method="post" action="recuperar_pass.php">
			<h2>Recuperació de contrasenya</h2>
			<br />
			<p>Introdueix el teu email:</p>
			<input type="text" name="mail" id="mail" />
			<br />
			<br />
			<input type="submit" name="button" id="button" value="Recuperar" />
		</form>
	</div>
	<!-- fi fons gris-->
</section>
<!-- fi contingut 1-->
<!--contingut 2-->
<section class="container">
	<div class="hero-unit"></div>
</section>
<!-- fi contingut 2-->
<hr>
<?php
if (isset($_POST['button'])) {
	if (isset($_POST['mail'])) {

		$mail = $_POST['mail'];

		$queEmp = "SELECT * FROM usuari WHERE email='$mail'";
		$resEmp = mysql_query($queEmp, $conexion) or die(mysql_error());
		$totEmp = mysql_num_rows($resEmp);
		if ($totEmp == 0) {
										echo "
				<script>
alert('El mail introduit no existeix');
				</script>
				";
			exit();
		}

		$row = mysql_fetch_assoc($resEmp);
		$hash = (($row['alias']) . ($row['contrasenya']));

		$headers = "From:Recuperar contrasenya <info@comparteixcoche.com>\r\n";
		$message = "Hola usuari, Aqui tens les teves dades. \n\n" . "Usuari: ". $row['alias']. "\n" . "Contrasenya: ". $row['contrasenya'].
		"\n\n" . "Tornar a la web fer click aqui:  http://localhost/Sintesis/index.php";

		if (mail($mail, "Recuperar contrasenya", $message, $headers)) {
												echo "
				<script>
alert('La teva contrasenya ha sigut enviada al teu correu.');
				</script>
				";
		} 
	}
}
?>
<?php
include ("includes/footer.php");
?>